package mysql.cs.command;

import common.Util.ByteUtil;
import mysql.Commond;
import org.apache.commons.lang.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

/**
 * SEMI_SYNC_ACK
 *   payload:
 *     1                  [ef]
 *     8                  log position
 *     string             log filename
 */
public class SemiSynCommand extends Commond {
    public long logPosition;
    public String logFilename;
    public SemiSynCommand(){
        setCommand((byte) 0xef);
    }

    @Override
    public void encode(byte[] data) {
        //不可能
    }

    @Override
    public byte[] decode() throws IOException {
        ByteArrayOutputStream out=new ByteArrayOutputStream();
        out.write(getCommand());
        ByteUtil.write8ByteUnsignedIntLittleEndian(logPosition,out);
        if(StringUtils.isNotEmpty(logFilename)){
            out.write(logFilename.getBytes());
        }
        return out.toByteArray();
    }

    public long getLogPosition() {
        return logPosition;
    }

    public void setLogPosition(long logPosition) {
        this.logPosition = logPosition;
    }

    public String getLogFilename() {
        return logFilename;
    }

    public void setLogFilename(String logFilename) {
        this.logFilename = logFilename;
    }
}
